import hashlib,requests
import uuid
# uuid = execjs.compile('''
# function uuid() {
#             let t, n, r = "";
#             for (t = 0; t < 32; t++) {
#                 n = 16 * Math.random() | 0,
#                 8 !== t && 12 !== t && 16 !== t && 20 !== t || (r += "-");
#                 const e = 3 & n
#                   , o = 16 === t ? 8 | e : n;
#                 r += (12 === t ? 4 : o).toString(16)
#             }
#             return r
#         }
# ''').call('uuid')

def getCookie():
    url = "https://fanyi.sogou.com/"
    params = {
        "keyword": f"{world}",
        "transfrom": "auto",
        "transto": "zh-CHS",
        "model": "general",
        "errcode": "s10"
    }
    response = requests.get(url, headers=headers, params=params)

    return response.cookies.get_dict()

def download(cookies):
    str = "auto" + "zh-CHS" +  world +'109984457'
    md5 = hashlib.md5()
    md5.update(str.encode('utf-8'))
    s = md5.hexdigest()

    url = "https://fanyi.sogou.com/api/transpc/text/result"
    data = {"from":"auto","to":"zh-CHS","text":world,"client":"pc","fr":"browser_pc","needQc":1,"s":s,"uuid": "9e823295-3931-4c18-8332-654579133cc8","exchange":"false"}
    response = requests.post(url, headers=headers,cookies = cookies,json=data)

    print(response.text)

if __name__ == '__main__':
    world = input('请输入翻译的词汇：')
    headers = {
        "Origin": "https://fanyi.sogou.com",
        "Referer": "https://fanyi.sogou.com/text?keyword=he&transfrom=auto&transto=zh-CHS&model=general",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    cookies = getCookie()
    download(cookies)
